#include "GenericHistogrammer.h"
#include <iostream>

GenericHistogrammer::~GenericHistogrammer()
{

}

//No preelection applied in orer to check if the program works correctly
void GenericHistogrammer::Execute()
{
	
//apply preselection here :
unsigned int n_cut=1;
  TH1FMap["eff"]->GetXaxis()->SetBinLabel(n_cut+1,"all");
  TH1FMap["eff"]->Fill(n_cut);
  n_cut++;
//delphes so called feature cut
if(evtdata->getNtrks()<=0){
return;
}
TH1FMap["eff"]->GetXaxis()->SetBinLabel(n_cut+1,TString("DelphesCheck(Ntrks>0)"));
TH1FMap["eff"]->Fill(n_cut);
n_cut++;
//*************************
int ectr,posctr;
int i_elmaxpt=0;
int i_posmaxpt=0;
for(unsigned int i=0;i<evtdata->getElectrons()->size();i++){
  if(evtdata->getElectrons()->at(i).Charge()<0){
    ectr++;
    if(evtdata->getElectrons()->at(i).P4().Pt()>evtdata->getElectrons()->at(i_elmaxpt).P4().Pt()){
      i_elmaxpt=i;
    }
  }
  else if(evtdata->getElectrons()->at(i).Charge()>0){
    posctr++;
    if(evtdata->getElectrons()->at(i).P4().Pt()>evtdata->getElectrons()->at(i_posmaxpt).P4().Pt()){
    i_posmaxpt=i;
    }
  }
  else std::cout<<"WARNING::GenericHistogrammer electron charge seems to be zero!"<<std::endl;
  
}
if(ectr==0 || posctr==0) return;

particle Zbos;
Zbos.setP4(evtdata->getElectrons()->at(i_elmaxpt).P4()+evtdata->getElectrons()->at(i_posmaxpt).P4());
FillHisto("ptZvsptJ0",Zbos.P4().Pt(),evtdata->getJets()->at(0).P4().Pt());
// endof cutflow ***************************


unsigned int Njets=evtdata->getJets()->size();
  unsigned int Nelect=ectr;
  unsigned int Nposit=posctr;
  unsigned int nJetsWctr=0;
  FillHisto("nJets",Njets);
  FillHisto("nelect",Nelect);
  FillHisto("nposit",Nposit);
 
  for(unsigned int i=0;i<Njets;i++){
   double ptj=evtdata->getJets()->at(i).P4().Pt();
   double mjet=evtdata->getJets()->at(i).P4().M();
   FillHisto("ptJets",ptj);
   FillHisto("etaphiJet",evtdata->getJets()->at(i).P4().Eta(),evtdata->getJets()->at(i).P4().Phi());
   if(mjet>60 && mjet<100){
   nJetsWctr++; 
   }
  }//end of jet loop
 
  //std::cout<<"evtdata->getJets()->size(): "<<evtdata->getJets()->size()<<std::endl; 
  FillHisto("nJetsWmass",nJetsWctr);

  if(Njets>0){
    FillHisto("mJ0",evtdata->getJets()->at(0).P4().M());
    FillHisto("ptJ0",evtdata->getJets()->at(0).P4().Pt());
    FillHisto("etaphiJ0",evtdata->getJets()->at(0).P4().Eta(),evtdata->getJets()->at(0).P4().Phi());
   }
 
 if(Njets>1){
   FillHisto("etaphiJ1",evtdata->getJets()->at(1).P4().Eta(),evtdata->getJets()->at(1).P4().Phi());
   FillHisto("ptJ1",evtdata->getJets()->at(1).P4().Pt());
   FillHisto("mJ1",evtdata->getJets()->at(1).P4().M());
 }
 return; 
}